package com.hupu.jrs.ui.forum

import android.net.Uri
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.daimajia.swipe.SwipeLayout
import com.facebook.drawee.view.SimpleDraweeView
import com.hupu.jrs.R
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter
import java.util.*
import javax.inject.Inject

/**
 * Created by sll on 2016/3/11.
 */
class ForumListAdapter @Inject
constructor() : RecyclerView.Adapter<ForumListAdapter.ViewHolder>(), StickyRecyclerHeadersAdapter<ForumListAdapter.HeaderViewHolder> {

    private var forums: MutableList<Forum> = ArrayList<Forum>()
    private var onItemClickListener: OnItemClickListener? = null


    fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
        this.onItemClickListener = onItemClickListener
    }

    interface OnItemClickListener {
        fun onForumDelAttentionClick(forum: Forum?)

        fun onForumOfflineClick(forum: Forum?)

        fun onForumClick(forum: Forum?)
    }

    fun bind(forums: MutableList<Forum>) {
        this.forums = forums
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.item_list_forum, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val forum = forums[position]
        holder.forum = forum
        if (!TextUtils.isEmpty(forum.logo)) {
            holder.ivIcon!!.setImageURI(Uri.parse(forum.logo))
        }
        holder.tvName!!.setText(forum.name)
        holder.swipeLayout!!.isSwipeEnabled = forum.forumId.equals("0")
    }

    override fun getHeaderId(position: Int): Long {
        return forums[position].weight!!.toLong()
    }

    override fun onCreateHeaderViewHolder(parent: ViewGroup): HeaderViewHolder {
        return HeaderViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.category_header, parent, false))
    }

    override fun onBindHeaderViewHolder(holder: HeaderViewHolder, position: Int) {
        holder.tvCategoryName!!.setText(forums[position].categoryName)
    }

    override fun getItemCount(): Int {
        return forums.size
    }

    fun remove(forum: Forum) {
        forums.remove(forum)
        notifyDataSetChanged()
    }

    inner class HeaderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        @BindView(R.id.tvCategoryName)
        var tvCategoryName: TextView? = null

        init {
            ButterKnife.bind(this, itemView)
        }
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        @BindView(R.id.ivIcon)
        var ivIcon: SimpleDraweeView? = null
        @BindView(R.id.tvName)
        var tvName: TextView? = null
        @BindView(R.id.swipeLayout)
        var swipeLayout: SwipeLayout? = null

        var forum: Forum? = null

        @OnClick(R.id.swipeLayout)
        fun swipeLayoutClick() {
            if (swipeLayout!!.openStatus == SwipeLayout.Status.Close) {
                if (onItemClickListener != null) {
                    onItemClickListener!!.onForumClick(forum)
                }
            }
        }

        @OnClick(R.id.tvDel)
        fun tvDelClick() {
            swipeLayout!!.close()
            if (onItemClickListener != null) {
                onItemClickListener!!.onForumDelAttentionClick(forum)
            }
        }

        @OnClick(R.id.tvOffline)
        fun tvOfflineClick() {
            swipeLayout!!.close()
            if (onItemClickListener != null) {
                onItemClickListener!!.onForumOfflineClick(forum)
            }
        }

        init {
            ButterKnife.bind(this, itemView)
        }
    }
}
